﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_ucThongtincanhangiaovien : System.Web.UI.UserControl
{
    BUS_Teacher objT = new BUS_Teacher();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["TeacherLogin"] != null)
        {
            Hienthi();
        }
    }
    public void Hienthi()
    {
        Page.Title = "Xem thông tin cá nhân";
        DateTime ngay;
        int id = int.Parse(Session["id"].ToString());
        var dataC = objT.Teacher_getAllByPid(id);
        txtid.Text = dataC.Rows[0]["id"].ToString().Trim();
        fullname.Text = dataC.Rows[0]["fullname"].ToString().Trim();
        username.Text = dataC.Rows[0]["teacherid"].ToString().Trim();
        address.Text = dataC.Rows[0]["adress"].ToString().Trim();
        ngay = Convert.ToDateTime(dataC.Rows[0]["birthday"].ToString().Trim());
        birthday.Text = ngay.ToString("dd/MM/yyyy");
        phone.Text = dataC.Rows[0]["phone"].ToString().Trim();
        string gioitinh = dataC.Rows[0]["gentle"].ToString().Trim();
        if (gioitinh == "Nam") dropgentle.SelectedIndex = 0; else dropgentle.SelectedIndex = 1;
        string hinh = dataC.Rows[0]["photo"].ToString().Trim();
        if (hinh == "")
        {
            photo.ImageUrl = "../Uploads/Teacher/no.png";
        }
        else photo.ImageUrl = "../Uploads/Teacher/" + hinh;
        //dataC.Rows[0]["Name"].ToString();
    }
    //Phuong thuc upload anh
    public bool UploadImage(string imagename)
    {
        bool complete = false;
        //Lay ve ten file anh
        string fname = uploadphoto.FileName;
        //Tach ten file
        string[] tokens = fname.Split(new char[] { '.' });
        //Luu tru dung luong file tinh bang byte
        int bytes = uploadphoto.FileBytes.Length;
        //Kiem tra dung luong file anh
        if (bytes > (1024 * 1024))
        {
            lberr.Text = "Dung lượng ảnh phải nhỏ hơn 4KB";
            return false;
        }
        //Kiem tra dang file upload
        if (tokens[tokens.Length - 1] == "jpg" || tokens[tokens.Length - 1] == "jpeg" || tokens[tokens.Length - 1] == "png" || tokens[tokens.Length - 1] == "gif")
        {
            //Khai bao bien luu tru duong dan den thu muc images
            //Response.Write("<script>alert(\""+Server+"\")</script>");
            string relPath = Server.MapPath("~\\");
            //Khai bao bien luu tru duong dan tuyet doi
            string absolutePath = relPath + "\\Uploads\\Teacher\\" + imagename;
            //Luu len server
            uploadphoto.SaveAs(absolutePath);
            complete = true;
        }
        else
        {
            lberr.Text = "Đây không phải file ảnh.";
            return false;
        }
        return complete;
    }
    protected void save_Click(object sender, EventArgs e)
    {
        DTO_Teacher dtoT = new DTO_Teacher();
        dtoT.ID = int.Parse(txtid.Text.ToString().Trim());
        dtoT.Fullname = fullname.Text.Trim();
        dtoT.Gentle = dropgentle.SelectedValue.ToString();
        //Response.Write("<script>alert(\"" + dtoC.ID + "," + dtoC.Fullname + "," + dtoC.Gentle + "\")</script>");
        dtoT.Adress = address.Text.Trim();
        DateTime strngay = DateTime.Parse(birthday.Text.Trim());
        dtoT.Birthday = strngay.ToString("MM/dd/yyyy");
        dtoT.Phone = phone.Text.Trim();
        dtoT.Status = 1;
        int id = int.Parse(txtid.Text.ToString().Trim());
        var dataT = objT.Teacher_getAllByPid(id);
        string hinh = dataT.Rows[0]["photo"].ToString().Trim();
        //Response.Write(Server.MapPath("~\\"));
        if (uploadphoto.FileName != string.Empty)
        {
            if (hinh == "")
            {
                Session["isChangePicteacher"] = true;
                dtoT.Photo = id + uploadphoto.FileName.Substring(uploadphoto.FileName.LastIndexOf(@"\") + 1);
            }
            else dtoT.Photo = hinh;
            if (!UploadImage(dtoT.Photo))
                return;
        }
        else Session["isChangePicteacher"] = false;
        if (!(bool)Session["isChangePicteacher"])
        {
            dtoT.Photo = hinh;
        }
        if (objT.Teacher_Update(dtoT))
        {
            Response.Write("<script>alert(\"Cập nhật thông tin thành công <..*!>\")</script>");
            Response.Redirect("Thongtincanhan.aspx");
        }
        else Response.Write("<script>alert(\"Cập nhật không thành công <..*!>\")</script>");
    }
}